<template>
  <div class="header">
    <div class="touxin banxin tanxin header_in">
      <section>
        <img class="logo" src="../assets/images/logo.png" alt="" />
      </section>
      <ul class="tanxin">
        <li @click="$router.push('/discover/home')" :class="{'active': $route.path === '/discover/home' || $route.path === '/' }">
          发现音乐<span></span>
        </li>
        <li @click="$router.push('/mymusic')" :class="{'active': $route.path === '/mymusic'}">
          我的音乐<span></span>
        </li>
        <li @click="$router.push('/friend')" :class="{'active': $route.path === '/friend'}">
          关注<span></span>
        </li>
        <li @click="$router.push('/product')" :class="{'active': $route.path === '/product'}">
          商城<span></span>
        </li>
        <li @click="$router.push('/musician')" :class="{'active': $route.path === '/musician'}">
          音乐人<span></span>
        </li>
        <li @click="$router.push('/download')" :class="{'active': $route.path === '/download'}">
          下载客服端<span></span>
        </li>
      </ul>
      <div class="search tanxin">
        <div class="search_btn">
          <img src="../assets/images/搜索小.png" alt="" width="14px" />
        </div>
        <input type="text" placeholder="音乐/视频/电商/用户" v-model="value" @keyup.enter="keyupFn" />
      </div>
      <div class="face">
        <!-- 登录头像 -->
        <img 
          v-if="$store.state.isHeader" 
          :src="profile.avatarUrl" 
          alt="" 
          class="avatar" 
          @click="$router.push(`/user/${profile.userId}`)" 
        />
        <!-- 未登录头像 -->
        <img
          src="../assets/images/头像 .png"
          alt=""
          height="40px"
          class="avatar"
          @click="goLogin"
          v-else
        />
      </div>
    </div>
    <div class="line"></div>
  </div>
</template>

<script>
import { UserDetailApi } from "@/request/api";
export default {
  data() {
    return {
      // 点击样式
      navActive: 1,
      // 输入框的值
      value: "",
      // 改变头像
      isHeader: false,
      // 用户id
      uid: 2081534865,
      profile: "",
    };
  },
  created() {
    UserDetailApi(this.uid).then((res) => {
      // console.log(res);
      this.profile = res.data.profile;
    });
    
  },
  methods: {
    keyupFn() {
      // 不能包含特殊字符
      let reg = /^((?!\\|\/|:|\*|\?|<|>|\||'|%|@|#|&|\$|\^|&|\*).){1,8}$/;
      if(this.value == "" || !reg.test(this.value)){
        let obj = { content: "搜索失败！", icon: "danger" };
        this.$store.dispatch("AsyncToast", obj);
      }else{
        let obj = { content: "搜索成功！", icon: "success" };
        this.$store.dispatch("AsyncToast", obj);
        this.$router.push(`/search/${this.value}`)
      }
    },
    goLogin() {
      // 获取本地储存用户的登录信息
      let token = localStorage.getItem('user');
      // 将获取到的数据转换成JSON对象
      token = JSON.parse(token);
      // 对获取到的数据进行处理
      console.log(token);
      if (token) {
        this.$store.commit("isHeaderShow", true);
        this.$store.commit("isLoginShow", true);
        this.$router.push("/mymusic");
        let obj = { content: "登录成功", icon: "success" };
        this.$store.dispatch("AsyncToast", obj);
      } else {
        // 需要改变登录那个框的v-show的状态
        this.$store.commit("changeLoginShow", true);
        // 提示登录
        let obj = { content: "请先登录", icon: "info" };
        this.$store.dispatch("AsyncToast", obj);
      }
    }
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";
.header {
  background: @black;
  width: 100%;
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  .logo {
    border-radius: 10px;
  }
  .header_in {
    height: 90px;
    display: flex;
    align-items: center;
    ul {
      width: 500px;
      li {
        box-sizing: border-box;
        font-size: 14px;
        color: #fff;
        line-height: 90px;
        padding: 0 15px;
        cursor: pointer;
        position: relative;
        &.active {
          color: #ffffff;
          background-color: rgba(0, 0, 0, 0.1);
          display: flex;
          justify-content: center;
          span {
            position: absolute;
            bottom: -10px;
            display: block;
            width: 30px;
            height: 30px;
            background: url(../assets/images/三角形.png) no-repeat 0 0 / 100% auto;
          }
        }
      }
    }
    .search {
      width: 160px;
      height: 35px;
      border-radius: 30px;
      background: #ffffff;
      overflow: hidden;
      .search_btn {
        width: 30px;
        height: 35px;
        display: flex;
        justify-content: center;
        align-items: center;
        overflow: hidden;
        transform: 1s;
        &:hover img {
          transform: scale(1.1);
          transition: 1s linear;
        }
      }
      input {
        width: 130px;
        height: 35px;
        outline: none;
        box-sizing: border-box;
        border-radius: 0 20px 20px 0;
        border: 0;
        font-size: 12px;
      }
    }
    .face {
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      .avatar {
        height: 45px;
        width: 45px;
        border-radius: 50%;
      }
    }
  }
  .line {
    height: 5px;
    width: 100%;
    background: @red;
  }
}
</style>